/*
 * Ext 表单展示 容器
 * 
 * */
 
 /**
  * 说明:
  *   var formContainer = new Ext.ux.form.FormContainer({

				  id:'id_formContainer',
				  width:all_width,
				  height:all_height-70,
				  pageSize:30,
			*	  bindingForm:oCenterPanel,//这里指定要绑定form,oCenterPanel为表单的引用
			*	  baseParams:{
 						key:'key_houseInfo',
 						village:Ext.getCmp('id_combo_village').getValue(),
 						street:Ext.getCmp('id_combo_street').getValue()
				   },//baseParams指定表单数据对应的valueList配置项
			*	  callback:function(record){
						    if(null!=record){
						      Ext.getCmp('houseNO_').setValue(record.houseno_);
						      Ext.getCmp('houseNO_').setValue(record.houseno_);
						      Ext.getCmp('houseNO_').setValue(record.houseno_);
						      Ext.getCmp('houseNO_').setValue(record.houseno_);
						      Ext.getCmp('houseNO_').setValue(record.houseno_);
						   
							}
					},//回调函数,可以对表单进行赋值操作,每次刷新表单数据的时候会调用一次,
			*       addHandler:function(){
			         	 .....这里是对表单的增加操作
			        },
			*        deleteHandler:function(){
			      	 	.....这里是对表单的增加操作
			         },
			*        saveHandler:function(){
			     		.....这里是对表单的增加操作
			         }
		 });
   */
   /*
     例:
       var formContainer = new Ext.ux.form.FormContainer({

				  id:'id_formContainer',
				  width:all_width,
				  height:all_height-70,
				  bindingForm:oCenterPanel,
				  baseParams:{
 						key:'key_houseInfo',
 						village:Ext.getCmp('id_combo_village').getValue(),
 						street:Ext.getCmp('id_combo_street').getValue()
				   },
				   callback:function(result){...通过result对表单进行赋值操作},
				   addHandler:insertNewRecord,//"增加记录"方法的引用
				   deleteHandler:deleteRecord,//"删除记录"方法的引用
				   saveHandler:saveRecord//"保存记录"方法的引用
				   });
				   
				   
				   function insertNewRecord(){
				     ....表单"增加记录"的逻辑
				   } 
				   function deleteRecord(){
				     ....表单"删除记录"的逻辑
				   } 
				   function saveRecord(){
				     ....表单"保存记录"的逻辑
				   } 
	*/
   
   
   
Ext.ns('Ext.ux.form');

Ext.ux.form.FormContainer = Ext.extend(Ext.Panel,{
	//初始化
	dataList:[],
	dataSize:0,
	isFresh:false,
	currentIndex:1,
	startSize:0,
	loadSize:this.loadSize||20,
	recordStartNo:0,
	recordEndNo:0,
	totalCount:0,
	ajaxUrl:'${pageContext.request.contextPath}/formContainer.table',
	me:null,
	initComponent:function(){
	  this.isFresh=false;
      me = this;
      this.width = this.width;
      this.layout = 'border';
      this.leftPanel = new Ext.Panel({
      frame:false,
      border:false,
      baseCls:'x-plain',
      bodyStyle:'background:#dfe8f6;border: 1px  #dfe8f6',
      width:50,
      height:this.height,
      buttonAlign:'center',
      items:[
	          {
		      	id:'id_btn_pre',
		      	xtype:'button',
		      	text:'<font size="6">上<br/><br/>一<br/><br/>页<br/><br/></font>',
		      	width:50,
		      	//iconCls:'x-tbar-page-prev',
		      	height:this.height,
		      	handler:this.doPagePre
		      	,listeners:{'afterrender':function(){keyListener({
				       component:this,
	                   opt:'customer',
	              	   keyCode:13,
	              	   callback:function(){
		      		   me.doPagePre();
		      	      }
	              	   })}}
		      
	          }]
      });
      this.rightPanel = new Ext.Panel({
      frame:false,
      baseCls:'x-plain',
      bodyStyle:'background:#dfe8f6;border: 1px  #dfe8f6',
      width:50,
      height:this.height,
      items:[
      		{
		      id:'id_btn_next',
		      height:this.height,
		      text:'<font size="6">下<br/><br/>一<br/><br/>页<br/><br/></font>',
		     
		      width:50,
		      xtype:'button',
		      //iconCls:'x-tbar-page-next',
		      handler:this.doPageNext,
				  listeners:{'afterrender':function(){keyListener({
					   component:this,
	              	   opt:'customer',
	              	   keyCode:13,
	              	   callback:function(){
					   me.doPageNext();
					  
				  }})}}
		    }
      	  ]
      });
      this.items = [
      	{
      	region:'center',
      	style:'margin-left:1px',
      	id:'centerItem',
      	border:false,
      	//width:this.width,
      //height:this.height-25,
      	items:[this.bindingForm]
      	},
       {
      	border:false,
       region:'west',
       width:50,
       height:this.height,
       items:[this.leftPanel]
       },
       {
       border:false,
       region:'east',
       width:50,
       height:this.height,
       items:[this.rightPanel]
       }
      ];
       var tbar = new Ext.Toolbar({
           style:'margin-bottom:1px',
    	   items:[
		        {
		         text: '新增',
		         iconCls:'x-formContainer-add',
		         handler:this.addHandler||function(){ExtWarning('你还没有指定 新增函数的引用')}
		         
				},'-',
				 {
		         text: '删除',
		         iconCls:'x-formContainer-delete',
		         handler:this.deleteHandler||function(){ExtWarning('你还没有指定 删除函数的引用')}
				},'-',
				{
		         text: '保存',
		         iconCls:'x-formContainer-save',
		         handler:this.saveHandler||function(){ExtWarning('你还没有指定 保存函数的引用')}
				},'-',
				{
					text: '查看列表',
			        iconCls:'x-formContainer-change',	
			        handler:this.listDisplayHandler||function(){ExtWarning('你还没有指定 转向列表函数的引用')}
				},
                {
					
					xtype:'label',
				
					html:'',
					id:'id_bbar_qu',
					//width:50,
					style:'padding:2 0 0 10;'
					
				},
				{
					xtype:'label',
				
					html:'',
					id:'id_bbar_street',
					//width:50,
					style:'padding:2 0 0 20;'
				},
				{
					xtype:'label',
					
					html:'',
					id:'id_bbar_village',
					//width:50,
					style:'padding:2 0 0 20;'
				},
				{
					
					xtype:'label',
					readOnly:true,
					html:'当前第',
					width:50,
					style:'padding:2 0 0 20;'
					
				},{
					id:'id_bbar_current',
					xtype:'label',
					html:'',
					style:'padding:2 0 0 20'
					
				},
				{
					xtype:'label',
					readOnly:true,
					html:'户， &nbsp;&nbsp; 共',
					width:50,
					style:'padding:2 0 0 20'
					
				},{
					id:'id_bbar_total',
					xtype:'label',
					html:'',
					style:'padding:2 0 0 20'
					
				},
				{
					
					xtype:'label',
					html:'户，&nbsp;&nbsp;其中：村民',
					style:'padding:2 0 0 20'
				},
				{
					id:'id_bbar_person_total',
					xtype:'label',
					html:'',
					style:'padding:2 0 0 20'
				}
				,
				{
					xtype:'label',
					html:'人，',
					style:'padding:2 0 0 20'
				},
				{
					
					xtype:'label',
					html:'跳转至&nbsp;&nbsp',
					style:'padding:2 0 0 15'
					
				},
				{
					id:'id_bbar_jumpto',
					xtype:'numberfield',
					width:50,
					style:'padding:2 0 0 0;text-align:left;',
					listeners:{'afterrender':function(){keyListener({
						   component:this,
                 	   opt:'customer',
                 	   keyCode:13,
                 	   callback:function(){
             	   			var jumpValue = Ext.getCmp('id_bbar_jumpto').getValue();
             	   			me.jumpTo(jumpValue);	
             	   			}})}}
						
				},
				{
					xtype:'button',
					text:'GO',
					iconCls:'go',
					width:50,
					style:'padding:2 0 0 20;',
					handler:function(){
					                me.isFresh=false;
									var jumpValue = Ext.getCmp('id_bbar_jumpto').getValue();
						      	 	me.jumpTo(jumpValue);
					}
				}
			]
       });
	  Ext.applyIf(this,{items:this.items,tbar:tbar});
	  Ext.ux.form.FormContainer.superclass.initComponent.call(this);
	
	 
	},
	afterRender:function(){
		
	 Ext.ux.form.FormContainer.superclass.afterRender.call(this);
	 if(this.checkNull(this.baseParams)&&this.checkNull(this.callback)){
	 	this.loadData(this.baseParams,this.callback);
	 }
	  
	 
   },
	doPagePre:function(){
	   this.isFresh=false;
	 if(me.currentIndex==1){
	        return false;  
	   	  }
		 if(me.currentIndex>me.recordStartNo){
	   	  me.currentIndex--;
	   	  me.fillData(me.currentIndex);
	   	   
	    }else{
	    //out of dataList
	    	
	    me.currentIndex--;
	    me.loadFormDatas(me.ajaxUrl,Ext.apply(me.baseParams,{start:me.recordStartNo-me.loadSize-1,limit:me.loadSize}),function(){});
	    
	    }
		

	},
	doPageNext:function(){
		this.isFresh=false;
		if(me.currentIndex==me.totalCount){
	        return false;  
	   	 }
	   if(me.currentIndex<me.recordEndNo){
	   	  me.currentIndex++;
	   	  me.fillData(me.currentIndex);
	   	   
	    }else{
	    	
	    //out of dataList
	     me.currentIndex++;
	     me.loadFormDatas(me.ajaxUrl,Ext.apply(me.baseParams,{start:me.recordEndNo,limit:me.loadSize}),function(){});
	    
	    }
	},
	//default
	loadData:function(_baseParams,_callback){
		var baseParams = {};
		var callback = function(){}
		if(this.checkNull(this.url))
		{
		    url = this.url ;
		    this.ajaxUrl = this.url;
		}
		if(this.checkNull(_baseParams)){
		    baseParams = _baseParams;
		}
		if(this.checkNull(_callback)){
		    callback = _callback ;
		}
	this.loadFormDatas(this.ajaxUrl,Ext.applyIf(_baseParams,{start:this.startSize,limit:this.loadSize}),this.callback);
    },
    freshData:function(){
      this.loadFormDatas(this.ajaxUrl,Ext.applyIf(this.baseParams,{start:this.startSize,limit:this.loadSize}),this.callback);

    },
    reload:function(fresh){
      this.isFresh=fresh;
      this.loadFormDatas(this.ajaxUrl,Ext.applyIf(this.baseParams,{start:this.startSize,limit:this.loadSize}),this.callback);
      
    },
  getBindingForm:function(){
	 return this.bindingForm;
	},
	getDataList:function(){
	 return this.dataList;
	},
	getDataSize:function(){
	return this.dataSize;
	},
	getTotalSize:function()
	{
		return this.totalCount;
	},
	checkNull:function(str){
	  if(null==str||''==str||typeof str =='undefined')
	  return false;
	  return true;
	},
	loadFormDatas:function(ajaxUrl,params,callback){
		me = this;
	  Ext.Ajax.request({             
	     url:ajaxUrl,
	     params:params,
	     success: function(response, options) {  
		  
	       var responseArray = Ext.util.JSON.decode(response.responseText);                                                     
	        if(responseArray.success==true){
	        	me.initDataInfo(responseArray);
	        	 if(me.isFresh)
				  {
					  
					  document.getElementById('id_bbar_current').innerHTML = me.totalCount>=1? '1':'0' ;
					  me.currentIndex=me.totalCount>=1? 1:0 ;
					  
					  
				  }
	        	if(null!=responseArray.data){
	        	   me.fillData(me.currentIndex);
	        	   if((Ext.getCmp('id_bbar_jumpto').getValue()!="")&&(Ext.getCmp('id_bbar_jumpto').getValue()>me.totalCount))
	        	   {
	        		   Ext.getCmp('id_bbar_jumpto').setValue(me.totalCount);
	        	   }
	        	}else{
	        	  callback(null); 
                }
	        } else if(responseArray.success==false){
	        	if(responseArray.type=='nodata'){
	        		me.initDataInfo(responseArray);
	         	  }else if(responseArray.type =='erro'){
	         	  	ExtWarning('查询异常,请检查数据库连接');
	         	  }
	        }
	        
	       
	    }        
		});  
	},
	initDataInfo:function(responseArray){
		
		
	   this.dataList = responseArray.data;
	   this.dataSize = this.dataList.length;
	    var totalSize =0;
	   if(this.dataSize>0){
		   this.personTotal=this.dataList[0].person_total;
	       totalSize = responseArray.totalCount;
	       this.totalCount = totalSize;
	   	   this.recordStartNo = this.dataList[0].recordIndex;
	   	   this.recordEndNo =  this.dataList[this.dataSize-1].recordIndex;
	   }else if(this.dataSize ==0){
		   this.personTotal=0;
	       this.totalCount = 0;
	   	   this.recordStartNo = 0;
	   	   this.recordEndNo = 0;
	   	   this.currentIndex = 0;
	   }
	   if(null!= document.getElementById('id_bbar_current')){
	   	 document.getElementById('id_bbar_current').innerHTML = this.currentIndex ;
	   }
	   if(null!= document.getElementById('id_bbar_total')){
		document.getElementById('id_bbar_total').innerHTML = totalSize ;	   
		}
	   if(null!= document.getElementById('id_bbar_person_total')){
			document.getElementById('id_bbar_person_total').innerHTML =  this.personTotal ;	   
			}
	   if(''!= t_street_name){
			document.getElementById('id_bbar_street').innerHTML = '<div style="padding-top:2px">'+t_street_name+"&nbsp;&nbsp;</div>" ;	   
			}
	   else
	   {
		   document.getElementById('id_bbar_street').innerHTML='';
	   }
	   if(''!= t_village_name){
			document.getElementById('id_bbar_village').innerHTML =  '<div style="padding-top:2px">'+t_village_name+"&nbsp;&nbsp;</div>" ;	   
			}
	   else
	   {
		   document.getElementById('id_bbar_village').innerHTML =  '' ;
	   }
	   if(''!= t_village_name)
	   {
		   document.getElementById('id_bbar_qu').innerHTML =  '' ;
	   }
	   else
	   {
		   document.getElementById('id_bbar_qu').innerHTML =  '<div style="padding-top:2px">东丽区</div>' ;
	   }
	   
	   
	 },
	 setBaseParams:function(params){
	   me.baseParams = params ;
	 },
	jumpTo:function(jumpValue){
	   if(me.totalCount ==0){
	   ExtWarning('没有数据');
	   return false;
	   }
	   if(''==jumpValue&&jumpValue!='0'){
	    ExtWarning('跳转记录数不能为空',function(){changeFocusTo('id_bbar_jumpto')});
	   	return false;
	   }
	   else if(jumpValue <=0){
	   ExtWarning('跳转记录数不能为负数或零,将显示第一条记录',function(){
	   	    me.currentIndex = 1;
			Ext.getCmp('id_bbar_jumpto').setValue(1);
			me.jumpTo(1);
	   return false;
	   });
	   }else if(jumpValue > me.totalCount){
	   	ExtWarning('跳转记录数大于最大记录数:'+me.totalCount+",将显示最后一条记录",function(){
	   	Ext.getCmp('id_bbar_jumpto').setValue(me.totalCount);
	   	me.jumpTo(me.totalCount);
	   	return false;
	   	});
	   }
	   else{
	   	   //正常跳转页 ,超出当前集合
	   	
	    if(jumpValue>this.recordEndNo||jumpValue<this.recordStartNo){
	     me.currentIndex = jumpValue ;
	     me.loadFormDatas(me.ajaxUrl,Ext.apply(me.baseParams,{start:me.loadSize*parseInt(jumpValue/me.loadSize),limit:me.loadSize}),function(){});
		}
		else{
	      	//在范围之内
	      	   var record = me.findRecord(jumpValue)
	      	   me.currentIndex = jumpValue;
	      	 document.getElementById('id_bbar_current').innerHTML = jumpValue ;
	  	   if(null!=record){
	      	    me.callback(record); 
	      	   }
	      	  
	      }
	   }
	
	},
	findRecord:function(value){
	  if(null!=this.dataList&&this.dataSize>0){
	          for(var i=0;i<this.dataList.length;i++){
	                 var record = this.dataList[i];
	                  for(var p in record){
	                      if(p=='recordIndex'&&record[p]==value){
	                         return record ;
	                      }
	                  }
	          }
	     }
	     return null;
	 },
	 fillData:function(recordIndex){
	 	 var record = me.findRecord(recordIndex);
	     if(null!=record){
	       me.callback(record);
	       me.changeBbarStatus(recordIndex);
	     }
	 },
	 changeBbarStatus:function(recordIndex){
	    document.getElementById('id_bbar_current').innerHTML = recordIndex;
	 
	 
	 }
	 
}); 
  Ext.reg('formcontainer',Ext.ux.form.FormContainer )